﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace webnew.UserControls
{
    public partial class UcCompany : System.Web.UI.UserControl
    {
        
        // 定义事件的委托
        public delegate void ChangedEventHandler(object sender, EventArgs e);
        // 定义一个下拉框选择事件
        public event ChangedEventHandler SelectedIndexChanged;            
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

       /// <summary>
       /// 显示单位列表
       /// </summary>
       /// <param name="isShowAll">如果为TRUE，添加空选项并设置当前单位为空选项</param>
        /// <param name="type">"OA"显示油库，“ALL”显示全部单位</param>
        public void show(bool isShowAll,string type)
        {
            if (type == "OA")
            {
                webnew.Common.CommonHelper.DataDdlBind(ddlCompany, new BLL.Sys_Company().GetALLYouKu(), "compname", "id");
                //ddlCompany.AutoPostBack = true;
                //ddlCompany_SelectedIndexChanged(null, null);  
            }
            else if (type == "ALL")
            {
                webnew.Common.CommonHelper.DataDdlBind(ddlCompany, new BLL.Sys_Company().GetAllCompany(), "compname", "id");
            }
            else
            {
                webnew.Common.CommonHelper.DataDdlBind(ddlCompany, new BLL.Sys_Company().GetHRCompany(), "compname", "id");

            }

            if (isShowAll) ddlCompany.Items.Insert(0, new ListItem("--请选择--", Guid.Empty.ToString()));
                
        }


        /// <summary>
        /// 显示单位列表（用于业务系统 列表页 所属单位 下拉框）
        /// </summary>
        /// <param name="isShowAll">如果为TRUE，添加空选项并设置当前单位为空选项</param>
        /// <param name="type">"OA"显示油库，“ALL”显示全部单位</param>
        public void show(string strCompany)
        {
            webnew.Common.CommonHelper.DataDdlBind(ddlCompany, new BLL.Sys_Company().GetALLYouKu(), "compname", "id");
            ddlCompany.Items.Insert(0, new ListItem("--请选择--", Guid.Empty.ToString()));
            ddlCompany.SelectedValue = strCompany;
            
            if (ddlCompany.SelectedValue == strCompany)
            {
                ddlCompany.Enabled = false;
            }          
        }

       
        /// <summary>
        /// 获取当前选中单位ID
        /// </summary>
        public string SelectedValue
        {
            get 
            {
                //if (ddlHuangPu.Items.Count > 0) return ddlHuangPu.SelectedValue;
                
                //else return ddlCompany.SelectedValue;
                return ddlCompany.SelectedValue;
            }
            set 
            { 

                ddlCompany.SelectedValue = value;
            }
        }

        /// <summary>
        /// 获取当前选中单位名称
        /// </summary>
        public string SelectedText
        {
            get { return ddlCompany.SelectedItem.Text; }
            set { ddlCompany.SelectedItem.Text = value; }
        }

        /// <summary>
        /// 设置是否回发事件
        /// </summary>
        public bool AutoPostBack
        {
            get 
            { 
                return ddlCompany.AutoPostBack; 
            }
            set
            {
                ddlCompany.AutoPostBack = value;
            }
        }

        public bool Enabled
        {
            set
            {
                ddlCompany.Enabled = value;
            }
        }

        public Unit Width
        {
            set
            {
                ddlCompany.Width = value; 
            }
        }
        /// <summary>
        /// 设置公司类型的选中值
        /// </summary>
        /// <param name="compType"></param>
        public void SetSelectedValue(string compType)
        {
            ddlCompany.SelectedValue = compType;
        }
        /// <summary>
        /// 下拉框选择事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        ///把自定义控件拉到具体页面，然后在Page_Load事件添加以下代码：
        ///UcCompany1.SelectedIndexChanged += UcCompany1_SelectedIndexChange; 
        ///然后添加一个事件： private void UcCompany1_SelectedIndexChange(object sender, EventArgs e){}
        protected virtual void ddlCompany_SelectedIndexChanged(object sender, EventArgs e)
        {
            //引发SelectedIndexChanged事件   
            //if (ddlCompany.SelectedValue != Guid.Empty.ToString())
            //{
            //    ddlHuangPu.Items.Clear();
            //    webnew.Common.CommonHelper.DataDdlBind(ddlHuangPu, new BLL.Sys_Company().GetNextCompany(ddlCompany.SelectedValue), "compname", "id");
            //    if (ddlHuangPu.Items.Count > 0)
            //    {
            //        ddlHuangPu.Visible = true;
            //    }
            //    else
            //        ddlHuangPu.Visible = false;
            //}
            //else
            //    ddlHuangPu.Visible = false;

            if (this.SelectedIndexChanged != null)
                this.SelectedIndexChanged(this, e);
        }
     
       
    }
}